#include <stdio.h>
#include <stdlib.h>
#include "list.h"

struct node *list_create(int size)
{
     int i = 1;
     struct node *p = malloc(sizeof(struct node));
     if (p == NULL)
          return NULL;
     p->next = p;
     p->data = i++;
     struct node *newnode, *current;
     current = p;
     while (i <= size)
     {
          newnode = malloc(sizeof(struct node));
          newnode->data = i++;
          newnode->next = p;
          current->next = newnode;
          current = newnode;
     }
     return p;
}

void list_show(struct node *first)
{
     struct node *current = first;
     while (current->next != first)
     {
          printf("%d     ", current->data);
          current = current->next;
     }
     printf("%d     ", current->data);
}
int func_kill(struct node **first,int size,int begin,int num)
{
     if (begin < 1 || begin > size)
          return -1;
     if(num < 1)
          return -2;
     int i = 1;
     struct node * current = *first;
     struct node * pre = current;
     while (current->data != begin)
     {
          pre = current;
          current = current->next;
     }
     while (current->next != current)
     {
          i = 1;
          while(i++ != num)
          {
               pre = current;
               current = current->next;
          }
          pre->next = current->next;
          printf("%d号出局\n",current->data);
          free(current);
          current = pre->next;
     }
     *first = current;
     printf("获奖的是%d号\n",current->data);
     return 0;
}